package ServerPackge;

import java.io.IOException;
import java.io.InputStream;

/**
 * 将请求信息封装成 Request 对象
 */
public class Request {
    /**
     * 请求方式 GET/POST
     */
    private String method;

    /**
     * / ,/index.html
     */
    private String url;

    /**
     * host名称
     */
    private String host;

    /**
     * 输入流，其他属性从输入流取值
     */
    private InputStream inputStream;


    public Request(InputStream inputStream) throws IOException {
        this.inputStream = inputStream;

        // 从输入流获取信息
        int count = 0;

        while(count == 0){
            count = inputStream.available();
        }

        byte[] bytes = new byte[count];
        inputStream.read(bytes);

        String inputStr = new String(bytes);

        //请求头
        String firstLineStr = inputStr.split("\\n")[0];  // GET / HTTP /1.1
        String[] strings = firstLineStr.split(" ");

        this.method = strings[0];
        this.url = strings[1];
        System.out.println("=======>>>method:" + method);
        System.out.println("=======>>>url:" + url);

        // 主机名
        String secondLineStr = inputStr.split("\\n")[1];
        String[] hostStrings = secondLineStr.split(" ");

        this.host = hostStrings[1].trim();
        System.out.println("=======>>>host:" + host);
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }
}
